package ar.com.android.alco.adapters;

import java.text.SimpleDateFormat;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import ar.com.android.alco.entities.ControlPeso;
import ar.com.android.alco.forms.R;

public class ListViewControlPesoAdapter extends ArrayAdapter<ControlPeso>
{
	public List<ControlPeso> items;
	Activity activity;
	
	public ListViewControlPesoAdapter(Activity activity, int textViewResourceId, List<ControlPeso> items) {
		super(activity, textViewResourceId, items);
		this.items = items;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public ControlPeso getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return (items.get(position)).getIdControlPeso();
	}

	private class ViewHolder {
		ImageView imgSelected;
	    TextView fechaControl;
	    TextView peso;
	    TextView creditosDisp;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		LayoutInflater inflater =  activity.getLayoutInflater();
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

		if (convertView == null)
		{
			convertView = inflater.inflate(R.drawable.lv_control_item, null);
			holder = new ViewHolder();
			holder.imgSelected = (ImageView) convertView.findViewById(R.id.imgSelected);
			holder.fechaControl = (TextView) convertView.findViewById(R.id.controlFecha);
			holder.peso = (TextView) convertView.findViewById(R.id.controlPeso);
			holder.creditosDisp = (TextView) convertView.findViewById(R.id.controlCreditos);

			convertView.setTag(holder);
		}
		else
			holder = (ViewHolder) convertView.getTag();

		ControlPeso controlPesoItem = items.get(position);
		if(controlPesoItem.getSelected())
			holder.imgSelected.setImageResource(R.drawable.ic_sel_item);
		else
			holder.imgSelected.setImageResource(R.drawable.ic_unsel_item);
		holder.fechaControl.setText(sdf.format(controlPesoItem.getFechaControl()));
		holder.peso.setText(controlPesoItem.getPeso().toString());
		holder.creditosDisp.setText(controlPesoItem.getCreditosDisp().toString());

		return convertView;	
	}

}
